package ms.pms.service;

import com.github.pagehelper.PageHelper;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.bean.BeanUtil;
import ms.common.util.string.StringUtil;
import ms.pms.bean.BindRoleParamBean;
import ms.pms.bean.CommonQueryParamBean;
import ms.pms.bean.UserParamBean;
import ms.pms.config.ResponseCode;
import ms.pms.dao.RoleDAO;
import ms.pms.dao.UserDAO;
import ms.pms.dao.UserRoleDAO;
import ms.pms.entity.Role;
import ms.pms.entity.User;
import ms.pms.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Project Name: ms
 * File Name: UserService
 * Package Name: ms.pms.service
 * Date: 2018/6/21  15:43
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class UserService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private RoleDAO roleDAO;

    @Autowired
    private UserRoleDAO userRoleDAO;

    /**
     * 查询用户信息
     * @param commonQueryParamBean
     * @return
     */
    public Page<User> query(CommonQueryParamBean commonQueryParamBean) {
        com.github.pagehelper.Page page = PageHelper.startPage(commonQueryParamBean.getPageNo(), commonQueryParamBean.getPageSize());
        List<User> users = userDAO.query(commonQueryParamBean.getStatus(), commonQueryParamBean.getName());
        return new Page(page.getPageNum(), page.getPageSize(), (int)page.getTotal(), users);
    }

    /**
     * 保存用户信息
     * @param userParamBean
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public void save(UserParamBean userParamBean) {
        Long id = userParamBean.getId();
        //查询用户是否存在
        User user = userDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(user)) {//新增
            User param = BeanUtil.copy(userParamBean, User.class);
            param.setCreateTime(System.currentTimeMillis());

            userDAO.insert(param);
            return;
        }
        //修改
        user.setName(userParamBean.getName());
        user.setTel(userParamBean.getTel());

        userDAO.updateByPrimaryKey(user);
    }

    /**
     * 启用
     * @param id
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public void use(Long id) {
        //查询用户是否存在
        User user = userDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(user)) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }
        if(1 == user.getStatus()) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户状态错误");
        }
        user.setStatus(1);
        userDAO.updateByPrimaryKey(user);
    }

    /**
     * 停用
     * @param id
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public void cancel(Long id) {
        //查询用户是否存在
        User user = userDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(user)) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }
        if(2 == user.getStatus()) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户状态错误");
        }
        user.setStatus(2);
        userDAO.updateByPrimaryKey(user);
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public void delete(Long id) {
        //查询用户是否存在
        User user = userDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(user)) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }
        if(1 == user.getStatus()) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户状态错误");
        }
        userDAO.deleteByPrimaryKey(user.getId());
//        user.setStatus(3);
//        userDAO.updateByPrimaryKey(user);
    }

    /**
     * 查询用户详情
     * @param id
     * @return
     */
    public User detail(Long id) {
        //查询用户是否存在
        User user = userDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(user)) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }
        return user;
    }

    /**
     * 绑定角色
     * @param bindRoleParamBean
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public void bind(BindRoleParamBean bindRoleParamBean) {
        User userParam = new User();
        userParam.setId(bindRoleParamBean.getUserId());
        userParam.setStatus(1);
        User user = userDAO.selectByPrimaryKey(userParam);
        if(StringUtil.isEmpty(user)) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }

        Role roleParam = new Role();
        roleParam.setId(bindRoleParamBean.getRoleId());
        Role role = roleDAO.selectByPrimaryKey(roleParam);
        if(StringUtil.isEmpty(role)) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "角色不存在");
        }

        UserRole param = new UserRole();
        param.setUserId(bindRoleParamBean.getUserId());
        //删除原有关系
        userRoleDAO.delete(param);

        UserRole userRole = BeanUtil.copy(bindRoleParamBean, UserRole.class);
        userRoleDAO.insert(userRole);
    }
}
